#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        if (nums.size() < 3) return res;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size()-2; ++i) {
            if (nums[i] > 0) break;
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            int left = i + 1, right = nums.size()-1;
            int oln = nums[left] - 1;
            while (left < right) {
                if (nums[left] == oln) {
                    ++left;
                    continue;
                }
                int tmp = nums[i] + nums[left] + nums[right];
                if (tmp > 0)
                    --right;
                else if(tmp<0)
                    ++left;
                else {
                    res.push_back({ nums[i],nums[left],nums[right] });
                    oln = nums[left];
                    ++left;
                    --right;
                }
            }
        }
        return res;
    }
};